/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.statemachine;

import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.uml2.uml.State;
import org.eclipse.uml2.uml.StateMachine;

import com.cea.papyrus.core.actions.AdaptDimension;
import com.cea.papyrus.core.actions.delete.DeleteFromDiagramAction;
import com.cea.papyrus.core.actions.delete.DeleteFromModelAction;
import com.cea.papyrus.core.editor.EditorContextMenuProvider;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.diagram.statemachine.editpart.RegionEditPart;
import com.cea.papyrus.diagram.statemachine.editpart.StateEditPart;
import com.cea.papyrus.diagram.statemachine.editpart.StateMachineEditPart;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;


/**
 * The Class StateMachineContextMenuProvider.
 * 
 * @author David Servat
 * @since 29 jan. 07
 */
class StateMachineContextMenuProvider extends EditorContextMenuProvider{

	/**
	 * The channel.
	 */
	private int channel = 501;

	/**
	 * Instantiate a new menu context provider for the specified EditPartViewer
	 * and ActionRegistry.
	 * 
	 * @param viewer the editor's graphical viewer
	 * @param modelManager the model manager
	 * @param registry the editor's action registry
	 */
	public StateMachineContextMenuProvider(EditPartViewer viewer, ActionRegistry registry, ModelManager modelManager){
		super(viewer, registry, modelManager);
	}

	/**
	 * Overriden to disable DELETE_FROM_MODEL_ACTION and DELETE_FROM_DIAGRAM_ACTION 
	 * for the last region of a statemachine and DELETE_FROM_MODEL_ACTION for a state machine if
	 * the diagram is owned by it.
	 * This is coherent with the restriction applied on the outline.
	 * 
	 * Also disable for the time being the automatic resize (rather complex to achieve).
	 * 
	 * @param menu 
	 * 
	 * @see com.cea.papyrus.editor.EditorContextMenuProvider#buildContextMenu(IMenuManager)
	 */
	@Override
	public void buildContextMenu(IMenuManager menu) {
		super.buildContextMenu(menu);

		//additional restrictions
		//on statemachines
		if(!getViewer().getSelectedEditParts().isEmpty() && 
				(getViewer().getSelectedEditParts().get(0) instanceof StateMachineEditPart)){
			//remove adapt dimension action: the code of this action needs to be updated to take into account region containers (state machines and states)
			menu.remove(AdaptDimension.ADAPT_DIMENSION_ACTION_ID);

			//remove delete from model action when the owner of the diagram is the statemachine itself
			StateMachineEditPart smEP = (StateMachineEditPart)getViewer().getSelectedEditParts().get(0);
			if(((Uml1SemanticModelBridge)((Diagram)smEP.getGraphNode().getContainer()).getOwner()).getElement().equals(smEP.getUmlElement()))
				menu.remove(DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_ID);
		}
		//on states
		if(!getViewer().getSelectedEditParts().isEmpty() && 
				(getViewer().getSelectedEditParts().get(0) instanceof StateEditPart)){
			//remove adapt dimension action: the code of this action needs to be updated to take into account region containers (state machines and states)
			menu.remove(AdaptDimension.ADAPT_DIMENSION_ACTION_ID);
		}

		//on regions
		if(!getViewer().getSelectedEditParts().isEmpty() && 
				(getViewer().getSelectedEditParts().get(0) instanceof RegionEditPart)){
			//remove adapt dimension action: the code of this action needs to be updated to take into account regions
			menu.remove(AdaptDimension.ADAPT_DIMENSION_ACTION_ID);

			GraphNode region = (GraphNode)((RegionEditPart)getViewer().getSelectedEditParts().get(0)).getGraphNode();
			GraphNode semParent = (GraphNode)region.getSemanticParent();

			//test whether this is the only region left of a statemachine
			//if this is the case remove delete from model and diagram actions: a statemachine has always one region and there is no interest in not showing it
			if(((Uml1SemanticModelBridge)(semParent.getSemanticModel())).getElement() instanceof StateMachine){
				if(semParent.getSemanticChild().size() == 1){
					menu.remove(DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_ID);
					menu.remove(DeleteFromDiagramAction.DELETE_FROM_DIAGRAM_ACTION_ID);
				}
			}
			//test whether this is the only region left of a (composite) state
			//if this is the case remove delete from diagram: in principle we could allow this, but it makes it clear that the state is composite
			//and prevents from managing peculiar graphical manipulation
			if(((Uml1SemanticModelBridge)(semParent.getSemanticModel())).getElement() instanceof State){
				if(semParent.getSemanticChild().size() == 1){
					menu.remove(DeleteFromDiagramAction.DELETE_FROM_DIAGRAM_ACTION_ID);
				}
			}
		}

	}


}
